package assql.logging
{
	import flash.utils.getQualifiedClassName;
        
    /**
     * Provides logging for asSQl
     **/
   	public class Logger
    {
        public static var LEVEL_NONE:int = 1000;
        public static var LEVEL_FATAL:int = 16;
        public static var LEVEL_ERROR:int = 8;
        public static var LEVEL_DEBUG:int = 4;
        public static var LEVEL_INFO:int = 2;
        public static var LEVEL_ALL:int = 0;
        
        /**
         * The current logging level. Default is NONE.
         **/
        public static var Level:int = LEVEL_NONE;
        
        /**
         * @private
         **/
        public static function Info(context:Object, message:String):void {
            log(LEVEL_INFO, context, message);
        }
        
        /**
         * @private
         **/
        public static function Debug(context:Object, message:String):void {
            log(LEVEL_DEBUG, context, message);
        }
        
        /**
         * @private
         **/
        public static function Error(context:Object, message:String):void {
            log(LEVEL_ERROR, context, message);
        }
        
        /**
         * @private
         **/
        public static function Fatal(context:Object, message:String):void {
            log(LEVEL_FATAL, context, message);
        }
        
        private static function log(msgLevel:int, context:Object, message:String):void {
            if ( Level <= msgLevel ) {
                    trace(pad(levelString(msgLevel) + " " + getQualifiedClassName(context)) + " : " + message);
            }
        }
        
        private static function pad(str:String):String {
            while ( str.length < 40 ) {
                    str += " ";
            }
            return str;
        }
        
        private static function levelString(whichLevel:int):String {
            switch (whichLevel) {
                    case LEVEL_NONE: 	return "";
                    case LEVEL_FATAL: 	return "[FATAL]";
                    case LEVEL_ERROR: 	return "[ERROR]";
                    case LEVEL_DEBUG: 	return "[DEBUG]";
                    case LEVEL_INFO: 	return "[INFO] ";
                    case LEVEL_ALL: 	return "[ALL]  ";
            }
            return "";
        }
    }
}